using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;

namespace Library.WebControls.SimpleSplitContainer {
  [PersistenceMode(PersistenceMode.InnerProperty)]
  [Bindable(false)]
  [ToolboxItem(false)]

  [PersistChildren(false)]
  [ParseChildren(false)]
  public class SpSpliterPanelCollection : IList<SpSpliterPanel>, IList {
    private readonly Table _tableContainer;
    private int _childIndex;
    internal SpSpliterPanelCollection(Table tableContainer) {
      _tableContainer = tableContainer;
      _tableContainer.Rows.Add(new TableRow());
      _tableContainer.Rows[0].Cells.Add(new TableCell());
    }

    private readonly IList<SpSpliterPanel> _list = new List<SpSpliterPanel>();
    public IEnumerator<SpSpliterPanel> GetEnumerator() {
      return _list.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator() {
      return GetEnumerator();
    }

    public void Add(SpSpliterPanel item) {
      _list.Add(item);
      if (item.ID == String.Empty) {
        _childIndex++;
        item.ID = "panel" + _childIndex;
      }
      _tableContainer.Rows[0].Cells[0].Controls.Add(item);
    }

    public int Add(object value) {
      Add((SpSpliterPanel)value);
      return Count;
    }

    public bool Contains(object value) {
      return Contains((SpSpliterPanel)value);
    }

    public void Clear() {
      _list.Clear();
    }

    public int IndexOf(object value) {
      return IndexOf((SpSpliterPanel)value);
    }

    public void Insert(int index, object value) {
      Insert(index, (SpSpliterPanel)value);
    }

    public void Remove(object value) {
      Remove((SpSpliterPanel)value);
    }

    public bool Contains(SpSpliterPanel item) {
      return _list.Contains(item);
    }

    public void CopyTo(SpSpliterPanel[] array, int arrayIndex) {
      _list.CopyTo(array, arrayIndex);
    }

    public bool Remove(SpSpliterPanel item) {
      _tableContainer.Rows[0].Cells[0].Controls.Remove(item);
      return _list.Remove(item);
    }

    public void CopyTo(Array array, int index) {
      CopyTo((SpSpliterPanel[])array, index);
    }

    public int Count {
      get { return _list.Count; }
    }


    public object SyncRoot {
      get { return ((IList)_list).SyncRoot; }
    }

    public bool IsSynchronized {
      get { return ((IList)_list).IsSynchronized; }
    }

    public bool IsReadOnly {
      get { return _list.IsReadOnly; }
    }

    public bool IsFixedSize {
      get { return ((IList)_list).IsFixedSize; }
    }

    public int IndexOf(SpSpliterPanel item) {
      return _list.IndexOf(item);
    }

    public void Insert(int index, SpSpliterPanel item) {
      _list.Insert(index, item);
      _tableContainer.Rows[0].Cells[0].Controls.AddAt(index, item);
    }

    public void RemoveAt(int index) {

      _list.RemoveAt(index);
      _tableContainer.Rows[0].Cells[0].Controls.RemoveAt(index);
    }

    object IList.this[int index] {
      get { return this[index]; }
      set { this[index] = (SpSpliterPanel)value; }
    }

    public SpSpliterPanel this[int index] {
      get { return _list[index]; }
      set { _list[index] = value; }
    }
  }

}
